/**
 * Created by Suzumiya on 16/07/03.
 */
(function () {
    "use strict";
    angular.module('News', [])
        .directive('alertNews', function (LoginInfo, Promise, $http, urlPrefix) {
            return {
                restrict: 'A',
                scope: {},
                link: function (scope) {
                    setTimeout(function reloadUnreadNews() {
                        $http.get(urlPrefix + "loadUnreadNews.do").then(function (resp) {
                            /**
                             * resp.data[0] 格式為：
                             * CONTENT : "弹窗测试4"
                             * FILE_INFO : null
                             * NAME : "系统管理员"
                             * NEWS_ID : "5779C2CC8D6CDBA9FA06E49C11513C21"
                             * PUBLISH_TIME : "2016-07-03 10:01"
                             * TITLE : "弹窗测试4"
                             */
                            var newsArray = resp.data;
                            (function showNews() {
                                if (newsArray.length == 0) {
                                    // 现场要求只需要打开页面查询一次即可
                                    // setTimeout(reloadUnreadNews, 10 * 1000);
                                    return;
                                }
                                var news = newsArray.splice(newsArray.length - 1, 1)[0];
                                var TITLE = news.TITLE || '';
                                if (TITLE.length > 10) {
                                    TITLE = TITLE.substring(0, 10) + " ...";
                                }

                                var CONTENT = news.CONTENT || '';
                                if (CONTENT.length > 50) {
                                    CONTENT = CONTENT.substring(0, 50) + " ...";
                                }
                                var FILE_INFO = news.FILE_INFO || '';
                                var fileStr = "";
                                if (FILE_INFO) {
                                    fileStr = '<span  style="color:blue;font-weight:bold;">[附件]</span>';
                                }
                                var detailUrl = urlPrefix+"loadAddNews.do?id=" + news.NEWS_ID + "&op=view&t=" + Date.now();
                                $.messager.show({
                                    id: 'news',
                                    title: '<div style="cursor:pointer" onclick="window.open(\''+detailUrl+'\')" title=' + TITLE + '>[公告]' + TITLE + '</div>',
                                    msg: '<div style="cursor:pointer" onclick="window.open(\''+detailUrl+'\')">' +
                                    '<span style="font-weight:bold;font-size:14px" >' + CONTENT + '</span>' + fileStr +
                                    '</br></br>' +
                                    '<span  style="font-weight:bold;float:right" >' + (news.NAME || '') + '</span></br>' +
                                    '<span  style="font-weight:bold;float:right" >' + (news.PUBLISH_TIME || '') + '</span></div>',
                                    timeout: 60000,
                                    showType: 'slide',
                                    height: 220,
                                    width: 300,
                                    modal: true,
                                    openShow: function () {
                                        $('.panel-header', this).css({backgroundColor: 'transparent'});
                                    },
                                    closeShow: function () {
                                        $http.post(urlPrefix + 'recordPopNews.do','',{params: {id: news.NEWS_ID}}).then(showNews, showNews);
                                    }
                                }).window("window").css({right: 10, width: 'auto'})
                            }());
                        })
                    }, 1);
                },
                template: '<div></div>'
            }
        })
}());